/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.intec.matsab.form;

import edu.intec.matsab.dao.CarreraDAO;
import edu.intec.matsab.dao.PensumDAO;
import edu.intec.matsab.persistence.Carrera;
import edu.intec.matsab.persistence.Pensum;
import edu.intec.matsab.xml.PensumXML;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.el.ELContext;
import javax.el.ValueExpression;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author matilde
 */
public class ManejoCarreraBean {

    
    private Carrera carrera;
    private List<Carrera> carrerasItems;
    private List<SelectItem> items;
    private boolean success;
    private boolean pensumSelected;
    private boolean carreraSelected;
    private PensumXML pensumXml;
    private Pensum pensum;
    private List<Pensum> listPensum;

    public ManejoCarreraBean() {
        items = new ArrayList<SelectItem>();
        carrera = new Carrera();
        listPensum = new ArrayList<Pensum>();
        carrerasItems = CarreraDAO.getAllCarreras();
        pensum = new Pensum();
        pensumXml = new PensumXML();

        crearItems();
    }

    public List<SelectItem> getItems() {
        return items;
    }

    public Carrera getCarrera() {
        return carrera;
    }

    public void setCarrera(Carrera carrera) {
        this.carrera = carrera;
    }

    public List<Carrera> getCarrerasItems() {
        return carrerasItems;
    }

    public void setCarrerasItems(List<Carrera> carrerasItems) {
        this.carrerasItems = carrerasItems;
    }

    public List<Pensum> getListPensum() {
        return listPensum;
    }

    public void setListPensum(List<Pensum> listPensum) {
        this.listPensum = listPensum;
    }

    public boolean isPensumSelected() {
        return pensumSelected;
    }

    public void setPensumSelected(boolean pensumSelected) {
        this.pensumSelected = pensumSelected;
    }

    public boolean isCarreraSelected() {
        return carreraSelected;
    }

    public void setCarreraSelected(boolean carreraSelected) {
        this.carreraSelected = carreraSelected;
    }

    public void setPensumXml(PensumXML pensumXml) {
        this.pensumXml = pensumXml;
    }

    public PensumXML getPensumXml() {
        return pensumXml;
    }

    public void setPensum(Pensum pensum) {
        this.pensum = pensum;
    }

    public Pensum getPensum() {
        return pensum;
    }

    public boolean isSuccess() {
        return success;
    }
 /* El metodo ELIMINAR, envia un objeto del tipo Carrera a la clase CarreraDAO
 *  para eliminar una carrera existente en la base de datos
 *
 *  @see CarreraDAO
*/
    public void eliminar(ActionEvent event) {
        success = CarreraDAO.eliminar(carrera);

    }


 /* El metodo insertar, envia un objeto del tipo Carrera a la clase CarreraDAO
 *  para agregar una carrera en la base de datos
 *
 *  @see CarreraDAO
*/
    public String insertar() {
        
        boolean insertar = CarreraDAO.insertar(carrera);

        if (insertar) {
            carrera = new Carrera();
        }

        return "";
    }

    private void crearItems() {
        Iterator itr = CarreraDAO.getAllCarreras().iterator();

        while (itr.hasNext()) {
            Carrera carrera = (Carrera) itr.next();
            items.add(new SelectItem(carrera.getIdCarrera(), carrera.getNombre()));
        }
    }

    public void selectCarrera() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String nRow = request.getParameter("carreraRow");

        if (!nRow.equals("")) {
            carreraSelected = true;
            carrera = carrerasItems.get(Integer.valueOf(nRow));
            listPensum = PensumDAO.buscar(carrera.getIdCarrera());
        }
    }

    public void verPensum() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String nRow = request.getParameter("pensumRow");

        if (!nRow.equals("")) {
            pensumSelected = true;
            pensum = listPensum.get(Integer.valueOf(nRow));
            pensumXml = PensumXML.toBean(pensum.getPensum());
        }
    }

    public void resetearBean(ActionEvent event) {
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        ValueExpression ve = FacesContext.getCurrentInstance().getApplication().getExpressionFactory().createValueExpression(elContext, "#{carreraBean}", ManejoCarreraBean.class);

        ManejoCarreraBean bean = new ManejoCarreraBean();
        ve.setValue(elContext, bean);
    }
}
